package com.wzy.Jdbc封装.service.Impl;

import com.wzy.Jdbc封装.dao.NewsDao;
import com.wzy.Jdbc封装.pojo.News_category;
import com.wzy.Jdbc封装.service.option_News_category;

import java.util.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class option_News_categoryImpl implements option_News_category {

    @Override
    public List<News_category> selectById(News_category category) throws SQLException {
        ResultSet resultSet =null;
        ArrayList<News_category> list = new ArrayList<>();
        try {
            String  sql = "SELECT * FROM `news_category` WHERE id=?";
            Object[] obj={category};
            resultSet = NewsDao.selectById(sql, obj);
            while(resultSet.next()) {
                News_category news_category = new News_category();

                int id1 = resultSet.getInt("id");
                String name = resultSet.getString(2);
                java.util.Date date = resultSet.getDate(3);

                news_category.setId(id1);
                news_category.setName(name);
                news_category.setDate(date);

                list.add(news_category);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            resultSet.close();
        }
        return list;
    }
//
    @Override
    public int deleteById(News_category category) {
        int rows = 0;
        String sql = "DELETE FROM `news_category`  WHERE id=? ";
        Object[] obj = {category};
        rows = NewsDao.delete(sql, obj);
        return rows;
    }

    @Override
    public int updateById(News_category category) {
        int rows = 0;
        String sql = "UPDATE `news_category` SET name=? WHERE id=?";
        Object[] obj = {category};
        rows = NewsDao.update(sql, obj);
        return rows;
    }

    @Override
    public int insertNews(News_category news) {
        int rows = 0;
        String sql = "INSERT INTO `news_category`(`id`,`name`,`createDate`) values(?,?,?)";
        Object[] obj = {news};
        rows = NewsDao.insert(sql, obj);
        return rows;
    }
}
